package com.efast.dmom.mes.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.aliyun.oss.ServiceException;
import com.efast.dmom.mes.bean.WmsInOutWarehouseLog;
import com.efast.dmom.mes.bean.WmsInWarehouseOrder;
import com.efast.dmom.mes.bean.WmsOutWarehouseOrder;
import com.efast.dmom.mes.bean.WmsStorage;
import com.efast.dmom.mes.dao.entity.PartDao;
import com.efast.dmom.mes.dao.entity.WmsInOutWarehouseLogDao;
import com.efast.dmom.mes.dao.entity.WmsStorageDao;
import com.efast.dmom.mes.entity.PartEntity;
import com.efast.dmom.mes.service.WmsWarehouseStorageService;
import com.efast.dmom.mes.util.SpringWebUtils;
import com.efast.dmom.mes.util.TimeUtil;
import com.efast.dmom.mes.util.WmsOrderStatusUtil;
@Service
@Transactional
public class WmsWarehouseStorageServiceImpl implements WmsWarehouseStorageService {
	
	@Autowired
	private WmsStorageDao wmsStorageDao;
	
	@Autowired
	private WmsInOutWarehouseLogDao wmsInOutWarehouseLogDao;
	
	@Autowired
	private PartDao partDao;
	
	/**
	 *  更新库存数据，并记录入库历史信息
	 * @param list
	 */
	@Override
	public void editInWarehouseStorage (List<WmsInWarehouseOrder> list) {
		if (list.size()>0) {
			for (WmsInWarehouseOrder wmsInWarehouseOrder : list) {
				WmsStorage wmsStorage = new WmsStorage();
				wmsStorage.setWarehouseBasId(wmsInWarehouseOrder.getWarehouseId());
				wmsStorage.setPartId(wmsInWarehouseOrder.getPartId());
				// 查询库存表中是否存在数据
				List<WmsStorage> selectByWarehouseAndPart = wmsStorageDao.selectByWarehouseAndPart(wmsStorage);
				if (selectByWarehouseAndPart.size() == 1) {
					//更新库存数据
					WmsStorage wmsStorage2 = selectByWarehouseAndPart.get(0);
					wmsStorage2.setStorageQuality(wmsStorage2.getStorageQuality() + wmsInWarehouseOrder.getInWarehouseQuality());
					wmsStorage2.setLastUpdateTime(TimeUtil.getLocalDateTime());
					wmsStorage2.setLastUpdateUser(SpringWebUtils.getRequestUser());
					wmsStorageDao.updateQuality(wmsStorage2);
					// 创建出入库历史
					createWarehouseStorageLog(wmsInWarehouseOrder);
				} else if (selectByWarehouseAndPart.size() == 0) {
					//保存库存数据
					wmsStorage.setStorageQuality(wmsInWarehouseOrder.getInWarehouseQuality());
					wmsStorage.setCreateTime(TimeUtil.getLocalDateTime());
					wmsStorage.setCreateUser(SpringWebUtils.getRequestUser());
					wmsStorage.setCompanyCode(SpringWebUtils.getRequestCompany());
					wmsStorageDao.save(wmsStorage);
					// 创建出入库历史
					createWarehouseStorageLog(wmsInWarehouseOrder);
				}
			}
		}
	}
	
	/**
	 *  更新库存数据，并记录出库历史信息
	 * @param list
	 * @throws Exception
	 */
	@Override
	public void editOutWarehouseStorage (List<WmsOutWarehouseOrder> list) {
		if (list.size()>0) {
			for (WmsOutWarehouseOrder apply : list) {
				WmsStorage wmsStorage = new WmsStorage();
				wmsStorage.setWarehouseBasId(apply.getWarehouseId());
				wmsStorage.setPartId(apply.getPartId());
				// 查询库存表中该仓库是否存在该零件
				List<WmsStorage> selectByWarehouseAndPart = wmsStorageDao.selectByWarehouseAndPart(wmsStorage);
				if (selectByWarehouseAndPart.size() == 1) {
					//更新库存数据
					WmsStorage wmsStorage2 = selectByWarehouseAndPart.get(0);
					if((wmsStorage2.getStorageQuality() - apply.getQuantity())<0){
						PartEntity byId = partDao.getById(apply.getPartId());
						throw new ServiceException("当前单据"+apply.getOutWarehouseOrder()+"下的物料"+ byId.getPartNo()+"库存不足！");
					}else{
						double sum = apply.getQuantity() * apply.getUnitPrice();
						// 扣减对应库存和总金额并更新库存数据
						if (wmsStorage2.getTotalAmount() - sum < 0) {
							throw new ServiceException("出库单"+ apply.getOutWarehouseOrder()+"对应仓库中的物料总金额不足！");
						}
						wmsStorage2.setTotalAmount(wmsStorage2.getTotalAmount() - sum);
						wmsStorage2.setStorageQuality(wmsStorage2.getStorageQuality() - apply.getQuantity());
						wmsStorage2.setLastUpdateTime(TimeUtil.getLocalDateTime());
						wmsStorage2.setLastUpdateUser(SpringWebUtils.getRequestUser());
						wmsStorageDao.updateQuality(wmsStorage2);
						// 创建出入库历史
						createWarehouseStorageLog(apply);
					}
				}else if(selectByWarehouseAndPart.size()>1){//查到多条库存，返回错误
					throw new ServiceException("foundMore_part_"+apply.getPartId()+"_ware_"+apply.getWarehouseId());
				}else{//查到空记录，返回错误
					throw new ServiceException("noSearch_part_"+apply.getPartId()+"_ware_"+apply.getWarehouseId());
				}
			}
		}
	}
	@Override
	public String checkStorageEnough(List<WmsOutWarehouseOrder> wmsOutWarehouseOrder){
		String mes = "";
		if(wmsOutWarehouseOrder!=null){
			for (WmsOutWarehouseOrder item : wmsOutWarehouseOrder) {
				List<WmsOutWarehouseOrder> checkStorageEnough = wmsStorageDao.checkStorageEnough(item);
				if (checkStorageEnough.get(0).getQuantity() - item.getQuantity() < 0) {
					 mes=checkStorageEnough.get(0).getWarehouseName()+"/仓库下的物料/"+checkStorageEnough.get(0).getPartName()+"数量不足！";
					return mes;
				}
			}
		}
		return mes;
	}
	@Override
	public String checkStorageEnough(WmsOutWarehouseOrder wmsOutWarehouseOrder){
		String mes = "";
		if(wmsOutWarehouseOrder!=null){
			List<WmsOutWarehouseOrder> checkStorageEnough = wmsStorageDao.checkStorageEnough(wmsOutWarehouseOrder);
			if (checkStorageEnough.get(0).getQuantity() > wmsOutWarehouseOrder.getQuantity()) {
				 mes=checkStorageEnough.get(0).getWarehouseName()+"/仓库下的物料/"+checkStorageEnough.get(0).getPartName()+"数量不足！";
				return mes;
			}
		}
		return mes;
	}
	// 创建入库历史
	public void createWarehouseStorageLog(WmsInWarehouseOrder wmsInWarehouseOrder) {
		WmsInOutWarehouseLog wmsInOutWarehouseLog = new WmsInOutWarehouseLog();
		wmsInOutWarehouseLog.setCompanyCode(SpringWebUtils.getRequestCompany());
		wmsInOutWarehouseLog.setCreateTime(TimeUtil.getLocalDateTime());
		wmsInOutWarehouseLog.setCreateUser(SpringWebUtils.getRequestUser());
		wmsInOutWarehouseLog.setInOutWarehouseId(wmsInWarehouseOrder.getId());
		wmsInOutWarehouseLog.setInOutWarehouseNo(wmsInWarehouseOrder.getInWarehouseOrder());
		wmsInOutWarehouseLog.setWarehouseId(wmsInWarehouseOrder.getWarehouseId());
		wmsInOutWarehouseLog.setOperation(WmsOrderStatusUtil.INWAREHOUSE);
		wmsInOutWarehouseLogDao.save(wmsInOutWarehouseLog);
	}
	// 创建出库历史
	public void createWarehouseStorageLog(WmsOutWarehouseOrder wmsOutWarehouseOrder) {
		WmsInOutWarehouseLog wmsInOutWarehouseLog = new WmsInOutWarehouseLog();
		wmsInOutWarehouseLog.setCompanyCode(SpringWebUtils.getRequestCompany());
		wmsInOutWarehouseLog.setCreateTime(TimeUtil.getLocalDateTime());
		wmsInOutWarehouseLog.setCreateUser(SpringWebUtils.getRequestUser());
		wmsInOutWarehouseLog.setInOutWarehouseId(wmsOutWarehouseOrder.getOutWarehouseId()==null?wmsOutWarehouseOrder.getId():wmsOutWarehouseOrder.getOutWarehouseId());
		wmsInOutWarehouseLog.setInOutWarehouseNo(wmsOutWarehouseOrder.getOutWarehouseOrder());
		wmsInOutWarehouseLog.setWarehouseId(wmsOutWarehouseOrder.getWarehouseId());
		wmsInOutWarehouseLog.setOperation(WmsOrderStatusUtil.OUTWAREHOUSE);
		wmsInOutWarehouseLog.setPartId(wmsOutWarehouseOrder.getPartId());
		wmsInOutWarehouseLog.setInOutWarehouseQuality(wmsOutWarehouseOrder.getQuantity());
		wmsInOutWarehouseLog.setInOutWarehouseRemark(wmsOutWarehouseOrder.getRemark());
		wmsInOutWarehouseLog.setUnit(wmsOutWarehouseOrder.getUnit());
		wmsInOutWarehouseLog.setUnitPrice(wmsOutWarehouseOrder.getUnitPrice());
		wmsInOutWarehouseLogDao.save(wmsInOutWarehouseLog);
	}

}
